package com.wonderful.compose.flow

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.distinctUntilChangedBy
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.EmptyCoroutineContext

/**
 * Describe:
 *
 * @author Wonderful
 * @time 2024/7/30 11:31
 */

fun flowLearn() = runBlocking {
    val scope = CoroutineScope(EmptyCoroutineContext)
    val job = launch {
        var count = 0
        while (true){
            println("count: ${count++}")
            delay(200)
        }
    }
//    val flow1 = flowOf(1, 2, 2, 3, 3, 3)
//    flow1.distinctUntilChanged().collect {
//        println(it)
//    }
//    val flow2 = flowOf("wonderful", "WONDERFUL", "wonderful123")
//    flow2.distinctUntilChanged { old, new -> old.uppercase() == new.uppercase() }.collect {
//        println(it)
//    }
//    flow2.distinctUntilChangedBy { it.uppercase() }.collect {
//        println(it)
//    }
//    println("------drop-------")
//    flow.drop(2).collect {
//        println(it)
//    }
//    flow.dropWhile { it < 3 }.collect {
//        println(it)
//    }
//    println("------take-------")
//    flow.take(2).collect {
//        println(it)
//    }
//    flow.takeWhile { it < 3 }.collect { println(it) }
//    println("------transform-------")
//    flow.transform {
//        if (it % 2 == 0) emit(it * it)
//    }.collect {
//        println(it)
//    }
}